package jdmx.framework.service;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

import jdmx.framework.common.HandlerFactory;
import jdmx.framework.service.interfaces.SqlProvider;
import jdmx.framework.service.interfaces.StaticContentLoader;
import jdmx.framework.service.patterns.typed.Orm;
import jdmx.library.da.Database;
import jdmx.library.da.Entity;
import jdmx.library.utils.LexicalFactory;

public class ServiceContext {
	
	public static LexicalFactory entityFactory = null;
	public static HandlerFactory ormFactory = null;
	public static HandlerFactory daoFactory = null;	
	public static HandlerFactory boFactory = null;
	public static HandlerFactory btFactory = null;
	
	public static ServiceConfiguration config = null;
	public static Logger logger = null;
	
	public static DatabaseManager databaseManager = null;
	public static StaticContentLoader staticContentLoader = null;
	public static SqlProvider sqlStatementProvider = null;

	/**
     * Initializeaza <code>ServiceContext</code>.
     * <br> 
     * <br/>Practic aici se executa toate initializarile aplicatiei server:
     * <ul>
     * 		<li>initializarea variabilei <code>logger</code></li>
     * 		<li>instantierea claselor de tip <code>Factory (BoFactory, DaoFactory, OrmFactory)</code>.</li>
     * 		<li>instantierea obiectului <code>config</code>.</li>
     * 		<li>incarcarea configuratiei din fisierele xml.</li>
     * 		<li>instantierea claselor de tip <code>Database</code>.</li>
     * 		<li>incarcarea datelor statice in <code>StaticContent</code>.</li>
     * </ul>
     * <br/>Nu intoarce nici o eroarea "checked" (? aici e o problema - todo de corectat)
     * <br/>
     * <br/>
	 */
	public static void init() {
		/*
		 * Initiaza logger-ul
		 * Initiaza content-ul static 
		 * Incarca configuratia locala
		 * Incarca bazele de date static in memorie
		 */
		DOMConfigurator.configure("conf/log4j.xml");
		StaticContent.init();
		
		/*
		 * Instantiaza dependintele
		 */
		if (entityFactory == null) entityFactory = new LexicalFactory(Entity.class, "", "");
		if (ormFactory == null) ormFactory = new HandlerFactory(Orm.class, config.ormPrefix, config.ormSuffix);
		if (daoFactory == null) daoFactory = new HandlerFactory(Dao.class, config.daoPrefix, config.daoSuffix);
		if (boFactory == null) boFactory = new HandlerFactory(Bo.class, config.boPrefix, config.boSuffix);
		if (btFactory == null) btFactory = new HandlerFactory(Bt.class, config.btPrefix, config.btSuffix);
		
		
		if (config == null) config = new ServiceConfiguration();
		config.readLocalConfiguration();
		
		if (logger == null) logger = Logger.getLogger("jdmx.Service");
		if (databaseManager == null) databaseManager =  new DatabaseManager();
		databaseManager.loadDatabases(config.dbInfos);
		
		
		/*
		 * Incarca content-ul static 
		 * (tabelele incarcate la init)
		 */
		if (staticContentLoader != null) {
			staticContentLoader.loadStaticContent();
		}
	}
	
	public static String getSql(String entityName, String statementName) {
		String sql = null;
		if (sqlStatementProvider != null) {
			sql = sqlStatementProvider.getSql(entityName, statementName);
		}
		return sql;
	}
	
	public static Database getDatabase(String key) { 
		return databaseManager.getDatabase(key);	
	}
}
